package org.subatu.orm.util;

/**
 * 大小写转化工具类
 */
public class ConvertUtils {

    /**
     * 驼峰转下划线
     *
     * @param camelCase
     * @return
     */
    public static String convertToSnakeCase(String camelCase) {
        if (camelCase == null) {
            return null;
        }

        String regex = "([a-z])([A-Z]+)";
        String replacement = "$1_$2";

        // 在每个小写字母和接下来的大写字母之间插入下划线
        String snakeCase = camelCase.replaceAll(regex, replacement).toLowerCase();

        return snakeCase;
    }

    /**
     * 下划线转驼峰
     *
     * @param underScoreStr
     * @return
     */
    public static String convertToCamelCase(String underScoreStr) {
        if (underScoreStr == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        boolean nextCharToUpper = false;

        for (char c : underScoreStr.toCharArray()) {
            if (c == '_' ) {
                nextCharToUpper = true;
            } else {
                if (nextCharToUpper) {
                    sb.append(Character.toUpperCase(c));
                    nextCharToUpper = false;
                } else {
                    sb.append(Character.toLowerCase(c));
                }
            }
        }

        return sb.toString();
    }

    // 下划线 转 类名 方式
    public static String convertToClassName(String underScoreStr) {
        String camelCase = convertToCamelCase(underScoreStr);
        return camelCase.substring(0, 1).toUpperCase() + camelCase.substring(1);
    }


    public static void main(String[] args) {
        String camelCaseExample = "thisIsAnExampleStringInCamelCase";
        System.out.println(convertToSnakeCase(camelCaseExample));
        // 输出: this_is_an_example_string_in_camel_case
    }
}
